/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package negocio;

import excepciones.UsuarioException;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.apache.commons.codec.binary.Hex;

/**
 *
 * @author diego
 */
@Entity
public class Usuario implements Serializable {
    @Id
    @Column(name="USUARIO_ID")
    private Integer id;
    @Basic
    private String userName;
    @Basic
    private String password;
    @Basic
    private String nivel;
    @Basic
    private boolean estado;
    @ManyToOne
    @JoinColumn(name="ROL_ID")
    private Rol rol;
    

    public Usuario() {
    }

    public Usuario(Integer id,String userName, String password, String nivel, Rol rol) throws UsuarioException, NoSuchAlgorithmException{       
        MessageDigest md;
        byte[] mb;
        
        md= MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        mb = md.digest();
        this.id = id;
        this.userName = userName;
        this.password = Arrays.toString(Hex.encodeHex(mb));
        this.nivel = nivel;
        this.estado = true;        
        this.rol = rol;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getNivel() {
        return nivel;
    }
    public void setNivel(String nivel) {
        this.nivel = nivel;
    }
    public boolean isEstado() {
        return estado;
    }
    public boolean getEstado() {
        return estado;
    }
    public void setEstado(boolean estado) {
        this.estado = estado;
    }
    public Rol getRol() {
        return rol;
    }
    public void setRol(Rol rol) {
        this.rol = rol;
    }
    
    public boolean validarPass(String unPassword) throws UsuarioException, NoSuchAlgorithmException{
        boolean var = false;
        MessageDigest md;
        byte[] mb;
        
        md= MessageDigest.getInstance("MD5");
        md.update(unPassword.getBytes());
        mb = md.digest();
        
        String unPasswordEncriptado  = Arrays.toString(Hex.encodeHex(mb)); 
        
        if(this.getPassword().equals(unPasswordEncriptado)){
            var = true;
        }else{
            throw new UsuarioException("Contraseña incorrecta. Verifique",1);
        }
        
        return var;
        
    }    
    public void desactivar(){
        this.estado = false;
    }    
    public void activar(){
        this.estado = true;
    }    
    @Override
    public String toString(){
        return this.userName;
    }
}
